﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Web;
using System.IO;

namespace CYZHAPI.Common.tools
{
    public class cryptionTool
    {
        /// <summary>
        /// 对字符串进行MD5运算
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMd5String(string str)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            MD5 md5 = MD5.Create();
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            byte[] md5Buffer = md5.ComputeHash(buffer);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in md5Buffer)
            {
                sb.Append(b.ToString("x2"));
            }
            md5.Clear();
            return sb.ToString();
        }

        /**//// <summary>
            /// DES加密
            /// </summary>
            /// <param name="encryptString"></param>
            /// <returns></returns>
        public static string DesEncrypt(string encryptString, string skey="DESkey1234")
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            byte[] keyBytes = Encoding.UTF8.GetBytes(skey.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }

        /**//// <summary>
        /// DES解密
        /// </summary>
        /// <param name="decryptString"></param>
        /// <returns></returns>
        public static string DesDecrypt(string decryptString, string skey="DESkey1234")
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            byte[] keyBytes = Encoding.UTF8.GetBytes(skey.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }

        /// <summary>
        /// 自定义密码加密
        /// </summary>
        /// <param name="sPlainText"></param>
        /// <returns></returns>
        public static String encryptPassword(String sPlainText)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            byte[] aryByte = { 0, 0 };
            byte[] aryBytePlainText = null;
            string st = null;
            try
            {
                for (int i = 0; i < sPlainText.Length; i++)//(int i = sPlainText.Length - 1; i >= 0; i--) 
                {
                    string sub = sPlainText.Substring(i, 1);
                    aryBytePlainText = System.Text.Encoding.GetEncoding("GBK").GetBytes(sub);
                    aryByte[0] = (byte)(((aryBytePlainText[0] & 0x0F)) | 0xB0);
                    aryByte[1] = (byte)(((aryBytePlainText[0] & 0xF0) >> 4) | 0xA0);
                    string s = System.Text.Encoding.GetEncoding("GBK").GetString(aryByte);
                    st += s;
                    //st.Insert( 0 ,s);
                }
            }
            catch (Exception ex)
            {
                string sss = ex.Message;
            }
            return st;
        }
        
        ///<summary>
        ///生成随机字符串 
        ///</summary>
        ///<param name="length">目标字符串的长度</param>
        ///<param name="useNum">是否包含数字，1=包含，默认为包含</param>
        ///<param name="useLow">是否包含小写字母，1=包含，默认为包含</param>
        ///<param name="useUpp">是否包含大写字母，1=包含，默认为包含</param>
        ///<param name="useSpe">是否包含特殊字符，1=包含，默认为不包含</param>
        ///<param name="custom">要包含的自定义字符，直接输入要包含的字符列表</param>
        ///<returns>指定长度的随机字符串</returns>
        public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNum == true) { str += "0123456789"; }
            if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        }
    }
}